import processing.serial.*;//library used for connecting to USB
import cc.arduino.*;  //library for interfacing arduino
import procontroll.*; // controller library
import net.java.games.input.*; //Java basis for the library

Arduino arduino; //declare the arduino 
ControllIO controllIO; //declare the USB device
ControllDevice Controller; // Declare the local Controller instance of the USB device

ControllSlider L3Y; //declare left vertical analog stick
ControllSlider L3X; //declare left horizontal analog stick
ControllSlider R3Y; //declare right vertical analog stick
ControllSlider R3X; //declare right horizontal analog stick

float totalLeftY; //value for the left analog stick
float totalLeftX;
float totalRightY; //value for the right analog stick
float totalRightX;

void setup(){
  size(250, 250); //specify window size
  
  arduino = new Arduino(this, Arduino.list()[0], 57600); //creating the instance of the arduino (last argument is the baud rate) [com port]
  controllIO = ControllIO.getInstance(this); // creating the instance of the USB device
  Controller = controllIO.getDevice("Controller (XBOX 360 For Windows)"); // linking the USB device with the local instance of the class (argument must be the specific name given by printDevices() function)
  Controller.setTolerance(0.1f); // setting the tolerance for the sticks... can fiddle with this later 
  
  arduino.analogWrite(9, 90); // make sure the motors are stop when program first run
  arduino.analogWrite(10, 90); // pin 9 and 10 are dedicated PWM for the Arduino Uno  

  L3Y = Controller.getSlider(0); //linking the slider of the Controller to the local instance of the class 
  L3X = Controller.getSlider(1);
  R3Y = Controller.getSlider(2); 
  R3X = Controller.getSlider(3);
  
  L3Y.setMultiplier(100); //multiplies float variable by 100 for ease of use
  L3X.setMultiplier(100);
  R3Y.setMultiplier(100);
  R3X.setMultiplier(100);
  
  //declaring the plug functions 
  Controller.plug(this, "PressA", ControllIO.ON_PRESS, 0);
  Controller.plug(this, "ReleaseA", ControllIO.ON_RELEASE, 0);
  
  Controller.plug(this, "PressB", ControllIO.ON_PRESS, 1);
  Controller.plug(this, "ReleaseB", ControllIO.ON_RELEASE, 1);
  
  Controller.plug(this, "PressX", ControllIO.ON_PRESS, 2);
  Controller.plug(this, "ReleaseX", ControllIO.ON_RELEASE, 2);
  
  Controller.plug(this, "PressY", ControllIO.ON_PRESS, 3);
  Controller.plug(this, "ReleaseY", ControllIO.ON_RELEASE, 3);
  
  Controller.plug(this, "PressLB", ControllIO.ON_PRESS, 4);
  Controller.plug(this, "ReleaseLB", ControllIO.ON_RELEASE, 4);
  
  Controller.plug(this, "PressRB", ControllIO.ON_PRESS, 5);
  Controller.plug(this, "ReleaseRB", ControllIO.ON_RELEASE, 5);
  
  Controller.plug(this, "PressBack", ControllIO.ON_PRESS, 6);
  Controller.plug(this, "ReleaseBack", ControllIO.ON_RELEASE, 6);
  
  Controller.plug(this, "PressStart", ControllIO.ON_PRESS, 7);
  Controller.plug(this, "ReleaseStart", ControllIO.ON_RELEASE, 7);
}

void draw(){
  // gets the current value of stick, uses the map() func to scale value from (-100 and 100) to (0 and 255) 
  totalLeftY = (255 - map(L3Y.getValue(), -100, 100, 0, 255));
  totalLeftX = (255 - map(L3X.getValue(), -100, 100, 0, 255));
  
  // converts float data to int data for the analogWrite to work properly
  int forwardAndBack = int(totalLeftY);
  int leftAndRight = int(totalLeftX);
  
  // ***MOTOR CONTROL***
  if (leftAndRight == 0){ //motors are in parallel, 2 for each side, turns only when stick is engaged at max left or right
    arduino.analogWrite(9, 255); 
    arduino.analogWrite(10, 0);
    println("Right Turn");
  }
    else if (leftAndRight == 255){ 
      arduino.analogWrite(9, 0);
      arduino.analogWrite(10, 255);
      println("Left Turn");
    }
    else{
      arduino.analogWrite(9, forwardAndBack-37); //move robot forward or back based on current value, allows for variable speed
      arduino.analogWrite(10, forwardAndBack-37); //subtracts 37 to equal 90, (my motor controller stops at 90)
      println(forwardAndBack-37); 
    }
}

//functions for button presses... can fill in with whatever you want
void PressA(){
  println("A Pressed");
}
void ReleaseA(){
  println("A Released");
}

void PressB(){
  println("B Pressed");
}
void ReleaseB(){
  println("B Released");
}

void PressX(){
  println("X Pressed");
}
void ReleaseX(){
  println("X Released");
}

void PressY(){
  println("Y Pressed");
}
void ReleaseY(){
  println("Y Released");
}

void PressLB(){
  println("LB Pressed");
}
void ReleaseLB(){
  println("LB Released");
}

void PressRB(){
  println("RB Pressed");
}
void ReleaseRB(){
  println("RB Released");
}

void PressBack(){
  println("Back Pressed");
}
void ReleaseBack(){
  println("Back Released");
}

void PressStart(){
  println("Start Pressed");
}
void ReleaseStart(){
  println("Start Released");
}


